package ph.com.gs3.formalistics.custom.implementation;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.custom.interfaces.CustomDocumentActivity;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.model.vo.business.document.Document;
import ph.com.gs3.formalistics.model.vo.business.document.DocumentAction;
import ph.com.gs3.formalistics.model.vo.business.form.Form;
import ph.com.gs3.formalistics.presenter.DocumentActivity;
import ph.com.gs3.formalistics.view.document.DocumentDynamicViewContentsManager;
import ph.com.gs3.formalistics.view.document.DocumentViewContentsManager;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SmartmaticDocumentActivity implements CustomDocumentActivity {

	public static final String TAG = SmartmaticDocumentActivity.class.getSimpleName();

	private DocumentActivity sourceActivity;

	private WebView mWebView;
	private List<PrintJob> mPrintJobs;

	public SmartmaticDocumentActivity(DocumentActivity sourceActivity) {
		this.sourceActivity = sourceActivity;
		mPrintJobs = new ArrayList<>();
	}

	@Override
	public void onBarcodeScanned(String fieldName, String scannedCode) {
		// TODO Auto-generated method stub

	}

	public List<DocumentAction> onCustomizeActions(List<DocumentAction> actions) {

		actions.add(new DocumentAction("Print", "Print"));

		return actions;
	}

	public DocumentViewContentsManager getDocumentViewContentsManager(Context context,
	        User currentUser, Form form) {

		FLLogger.d(TAG, "Form web id: " + form.getWebId());

		if (SmartmaticDocumentViewContentsManager.HARDWARE_TEST_FORM_ID.equals(form.getWebId())) {
			((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			return new SmartmaticDocumentViewContentsManager(context);
		}

		return new DocumentDynamicViewContentsManager(context, currentUser);

	}

	public boolean customSubmitDocumentAction(Form form, Document document, String action) {
		WebView webView = new WebView(sourceActivity);
		webView.setWebViewClient(new WebViewClient() {

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return false;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				FLLogger.d(TAG, "page finished loading " + url);
				createWebPrintJob(view);
				mWebView = null;
			}
		});

		// Generate an HTML document on the fly:
		String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, "
		        + "testing, testing...</p></body></html>";
		webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

		// Keep a reference to WebView object until you pass the PrintDocumentAdapter
		// to the PrintManager
		mWebView = webView;

		return false;
	}

	private void createWebPrintJob(WebView webView) {

		// Get a PrintManager instance
		PrintManager printManager = (PrintManager) sourceActivity
		        .getSystemService(Context.PRINT_SERVICE);

		// Get a print adapter instance
		PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

		// Create a print job with name and adapter instance
		String jobName = sourceActivity.getString(R.string.app_name) + " Document";
		PrintJob printJob = printManager.print(jobName, printAdapter,
		        new PrintAttributes.Builder().build());

		// Save the job object for later status checking
		mPrintJobs.add(printJob);
	}

}
